﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S.Authorization.Common;
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.Authority;
using S.Authorization.Infrastructure.Interface.User;
using S.Authorization.Read.API.Dto;
using System.Security.Claims;
using static S.Authorization.Read.API.Application.Command.MenuCommand.MenuReadAllCommand;
using static S.Authorization.Read.API.Application.Command.RoleCommand.RoleReadAllCommand;

namespace S.Authorization.Read.API.Controllers
{
    /// <summary>
    /// 角色和菜单--读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleMenuReadController : BaseController
    {
        #region 注入
        public RoleMenuReadController(IMediator mediator) : base(mediator)
        {
        }

        #endregion

        /// <summary>
        /// 显示当前用户下的菜单
        /// </summary>
        /// <returns>返回该用户下的权限菜单</returns>
        [Authorize]
        [HttpGet]
        public async Task<List<MenuItemDto>> GetMenuListAsyncs()
        {

            return await mediator.Send(new MenuGetByUIdCommand()
            {
                PId=0,
                UserId =UserStrategyDto.UserId
            });
        }
        /// <summary>
        /// 显示递归的菜单
        /// </summary>
        /// <param name="pId">当前用户登录ID</param>
        /// <returns>返回该用户下的权限菜单</returns>
        [HttpGet]
        public async Task<List<int>> GetMenuListByPId(int pId=0,int roleId=0)
        {
            return await mediator.Send(new MenuGetByPIdCommand()
            {
                PId = pId,
                RoleId= roleId
            });
        }

        /// <summary>
        /// 角色表分页显示
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns>返回分页后的结果</returns>
        [HttpGet]
       //[Authorize]
        public async Task<Pager<RoleModel>> GetPageRole(int pageIndex=1,int pageSize=15,string? roleName="")
        {
            return await mediator.Send(new RoleGetPageAllCommand()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                RoleName=roleName
            });
        }
        /// <summary>
        /// 角色显示
        /// </summary>
        /// <returns></returns>
        //[Authorize]
        [HttpGet]
        public async Task<List<RoleModel>> GetRole()
        {
            return await mediator.Send(new RoleGetAllCommand()
            {
               
            });
        }
        /// <summary>
        /// 角色表反填
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回当前Id对应的类</returns>
        //[Authorize]
        [HttpGet]
        public async Task<RoleModelByRIdDto> GetRoleById(int roleId)
        {
            return await mediator.Send(new RoleGetByIdCommand()
            {
                RoleId = roleId
            });
        }

        /// <summary>
        /// 获取每个菜单下的所有功能
        /// </summary>
        /// <param name="menuId">菜单Id</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>返回分页查询后的数据</returns>
        //[Authorize]
        [HttpGet]
        public async Task<List<MenuItemDto>> GetMenuAndOperation(int menuId=0)
        {
            return await mediator.Send(new MenuAndOperationGetCommand()
            {
                MenuId = menuId,
            });
        }

        /// <summary>
        /// 显示菜单功能角色三表
        /// </summary>
        /// <param name="menuId">菜单ID</param>
        /// <returns>返回菜单ID，菜单Name，角色ID，角色Name，功能名称</returns>
        //[Authorize]
        [HttpGet]
        public async Task<Pager<FeatureMenuRoleDto>> GetFeatureMenuRole(int menuId=0,int pageIndex=1,int pageSize=15)
        {
            return await mediator.Send(new FeatureMenuRoleGetCommand()
            {
                MenuId = menuId,
                PageIndex= pageIndex,
                PageSize= pageSize
            });
        }

        /// <summary>
        /// 显示菜单表ID和Name
        /// </summary>
        /// <returns></returns>
        //[Authorize]
        [HttpGet]
        public async Task<List<GetMenuListDto>> GetMenuAll()
        {
            return await mediator.Send(new MenuGetAllCommand());
        }


        /// <summary>
        /// 菜单表--根据MId获取PId
        /// </summary>
        /// <param name="mIds">主键Id</param>
        /// <returns>返回该数据的父级</returns>
        [HttpGet]
        public async Task<List<int>> GetMenuPIdByMId(string mIds)
        {
            return await mediator.Send(new MenuGetPIdByMIdCommand()
            {
                MIds = mIds
            });
        }
    }
}
